<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { ToastWarning, Toastloading } from '@/utils/public';
  import { propApi } from '@/api/controller';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['submit'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    listTableIndex:-1,
    giftList:[] as Array<any>,
    giftListLoading:false
  })
  const typeData = [
    { name: "礼物", type: 1 },
  ]
  interface formQuert {
    id:number
    price:number
    type:number
    num:number
    time:number
    name:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    type: [{ required: true, trigger: 'blur', message: '请选择礼物类型' }],
    id: [{ required: true, trigger: 'blur', message: '请选择礼物' }],
    num: [{ required: true, trigger: 'blur', message: '请输入礼物数量' }],
    time: [{ required: true, trigger: 'blur', message: '请输入时间限制(秒)' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      form.time = form.time * 1
      form.num = form.num * 1
      $emit('submit',action.listTableIndex,form)
      closeDialog()
    })
    
  }
  const getGiftList = async()=>{
    action.giftListLoading = true
    const res = await propApi.getGifts({limit:99999})
    action.giftListLoading = false
    const { data=[] } = res.data
    action.giftList = data
  }
  /**
   * 选择奖励类型
   */
  const typeChange = ()=>{
    getGiftList()
  }
  /**
   * 选择礼物
   */
  const giftChangeData = (type:any = 0)=>{
    const { price=0, name } = action.giftList.find(res=>res.gift_id == type)
    form.price = price
    form.name = name
  }
  const openDialog = async(query:any = {})=> {
    const { id='', index=-1, name='', num='', price='', time='', type='' } = query
    if(id) {
      const loading = Toastloading()
      await getGiftList()
      loading.close()
    }
    action.listTableIndex = index
    Object.assign(form,{
      id,
      name,
      num,
      price,
      time,
      type
    })
    action.title = id?'编辑礼物':'新增礼物'
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    action.listTableIndex = -1
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    destroy-on-close
    width="60%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules" label-position="top">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="礼物类型" prop="type">
              <el-select v-model="form.type" @change="typeChange" placeholder="请选择礼物类型">
                <el-option
                  :label="item.name"
                  :value="item.type"
                  v-for="(item, index) in typeData"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item :label="`礼物名称`" prop="id">
              <el-select
                v-model="form.id"
                placeholder="请选择礼物"
                filterable
                :loading="action.giftListLoading"
                @change="giftChangeData"
              >
                <!-- 礼物选项 -->
                <el-option
                  :label="item.name"
                  :value="item.gift_id"
                  v-for="(item, index) in action.giftList"
                  :key="index"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="礼物价格" prop="price">
              <el-input
                v-model="form.price"
                disabled
                placeholder="价格"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="礼物数量" prop="num">
              <el-input
                v-model="form.num"
                placeholder="请输入礼物数量"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="时间限制(秒)" prop="time">
              <el-input
                v-model="form.time"
                placeholder="请输入时间限制(秒)"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>